<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: User.php 130 2009-05-21 14:23:24Z movoin $
 */

//{{include
FLEA::loadClass('Controller_Base');
//}}

class Controller_User extends Controller_Base
{

    var $modelUser;

    function Controller_User()
    {
        parent::Controller_Base();
        $this->modelUser =& FLEA::getSingleton( 'Model_User' );
    }

    function actionIndex()
    {
        $this->_setBack();
        $data['focus'] = 'user';
        $data['column'] = $this->getColumnInf( 'user' );
        //读取用户列表
        $group_id = $_GET['group']?(int)$_GET['group']:null;
        $data['rows'] = $this->modelUser->getAllUsers($group_id);
        $this->_executeView('user', $data);
    }

    /**
     * 新增用户
     *
     */
    function actionAdd()
    {
        $this->_setBack();
        $table = & $this->modelUser->getTable();
        $row = $this->_prepareData($table->meta);
        $this->_editUser($row);
    }
    
    /**
     * 编辑用户
     *
     */
    function actionEdit()
    {
        $this->_setBack();
        $row = $this->modelUser->getUser((int)$_GET['userid']);
        $this->_editUser($row);
    }
    
    /**
     * 保存用户
     *
     */
    function actionSave()
    {
        js_alert($this->modelUser->saveUser($_POST), 0, $this->_getBack());
    }
    
    /**
     * 删除用户
     *
     */
    function actionRemove()
    {
        js_alert($this->modelUser->removeUser((int)$_GET['userid']), 0, $this->_getBack());
    }

     /**
      * 用户组
      *
      */
    function actionGroup()
    {
        $this->_setBack();
        $data['focus'] = 'user';
        $data['column'] = $this->getColumnInf( 'user' );
        $data['rows'] = $this->modelUser->getAllGroups((int)$_GET['site']);
        $this->_executeView('usergroup', $data);
    }

     /**
      * 添加用户组
      *
      */
    function actionAddGroup()
    {
        $this->_setBack();
        $table = & $this->modelUser->getTable('group');
        $row = $this->_prepareData($table->meta);
        $this->_editUserGroup($row);
    }

    /**
     * 编辑用户组
     *
     */
    function actionEditGroup()
    {
        $this->_setBack();
        $row = $this->modelUser->getGroup((int)$_GET['groupid']);
        $this->_editUserGroup($row);
    }

     /**
      * 保存用户组
      *
      */
    function actionSaveGroup()
    {
        js_alert($this->modelUser->saveGroup($_POST), 0, $this->_getBack());
    }

    /**
     * 删除用户组
     *
     */
    function actionRemoveGroup()
    {
        js_alert($this->modelUser->removeGroup((int)$_GET['groupid']), 0, $this->_getBack());
    }

    /**
     * 编辑用户
     *
     */
    function _editUser(&$row)
    {
        $data['focus'] = 'user';
        $data['column'] = $this->getColumnInf( 'user' );
        FLEA::loadHelper('Array');
        $data['group'] = array_to_hashmap($this->modelUser->getAllGroups((int)$_GET['site'], 'group_id, name'), 'name', 'group_id');
        $data['row'] = $row;
        $this->_executeView('user-edit', $data);
    }

     /**
      * 编辑用户组
      *
      */
    function _editUserGroup(&$row)
    {
        $data['focus'] = 'user';
        $data['column'] = $this->getColumnInf( 'user' );
        $data['row'] = $row;
        $this->_executeView('usergroup-edit', $data);
    }

}
